package br.com.biblioteca.dao;

import br.com.biblioteca.exceptions.NotFoundException;
import java.util.ArrayList;
import java.util.List;
import br.com.biblioteca.vo.User;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;



/**
 *
 * @author João Sávio Ceregatti Longo - joaosavio@gmail.com
 */
public class UserDAO extends GenericHibernateDAO<User, Long> implements IUserDAO {
      /*  Always return something, including null
     *  Don't close the session!
     */
    public User getByUnique(User user) {
       String hql = "select t from User as t where t.login = :login";
       Query q = getSession().createQuery(hql);
       q.setParameter("login", user.getLogin());
       return (User) q.uniqueResult();
    }

    public User get(String login, String password) throws NotFoundException {
        List<Criterion> list = new ArrayList<Criterion>();
        Criterion c = Restrictions.eq("login", login);
        list.add(c);
        c = Restrictions.eq("password", password);
        list.add(c);
        List<User> resultList = findByCriteria(list, null);
        if (resultList == null || resultList.size() == 0) {
            return null;
        }
        return resultList.get(0);
    }
}
